package org.wbd.pklib.web.beans;

import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.wbd.pklib.db.Book;
import org.wbd.pklib.db.Item;
import org.wbd.pklib.db.Loan;
import org.wbd.pklib.db.Repo;
import org.wbd.pklib.db.User;
import org.wbd.pklib.db.consts.Categories;
import org.wbd.pklib.db.logic.LoanLogic;
import org.wbd.pklib.db.logic.LogicRespond;
import org.wbd.pklib.db.logic.RepoLogic;
import org.wbd.pklib.db.logic.SuggestionLogic;
import org.wbd.pklib.db.logic.UserLogic;

public class UserBean {
	private String login;
	private String password;
	private String function;
	private User loggedUser;
	private Repo selectedRepo;
	private boolean logged;
	private String currentTab= "browse";

	private String result;
	private ArrayList<Item> loanList;
	private ArrayList<Repo> userRepoList;
	private ArrayList<Repo> availableRepos;
	private ArrayList<Item> myBooks;
	private ArrayList<Book> suggestions;
	
	public String getCurrentTab() {
		return currentTab;
	}

	public void setCurrentTab(String currentTab) {
		this.currentTab = currentTab;
	}
	
	

	public ArrayList<Book> getSuggestions() {
		return suggestions;
	}

	public void setSuggestions(ArrayList<Book> suggestions) {
		this.suggestions = suggestions;
	}

	public ArrayList<Item> getLoanList() {
		return loanList;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}


	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}


	public String getFunction() {
		return function;
	}

	public void setFunction(String function) {
		this.function = function;
	}

	public User getLoggedUser() {
		return loggedUser;
	}

	public void setLoggedUser(User loggedUser) {
		this.loggedUser = loggedUser;
	}

	public boolean isLogged() {
		return logged;
	}

	public void setLogged(boolean logged) {
		this.logged = logged;
	}

	public String[] getBookCategories() {
		return Categories.BOOK_CATEGORIES;
	}

	public Repo getSelectedRepo() {
		return selectedRepo;
	}

	public void setSelectedRepo(Repo selectedRepo) {
		this.selectedRepo = selectedRepo;
	}
	
	public ArrayList<Repo> getUserRepoList() {
		return userRepoList;
	}
	
	public ArrayList<Repo> getAvailableRepos() {
		return availableRepos;
	}

	
	public ArrayList<Item> getMyBooks() {
		return myBooks;
	}
	
	public String logIn() {
		FacesContext.getCurrentInstance().getExternalContext().getFlash()
				.setKeepMessages(false);
		LogicRespond logicRespond = UserLogic.getInstance().logIn(this.login,
				this.password);
		if (logicRespond.isSuccesful()) {
			this.loggedUser = (User) logicRespond.getResultObject();
			this.logged = true;
			createLoanList();
			createUserRepoList();
			createAvailableRepos();
			createMyBooks();
			FacesContext.getCurrentInstance().getExternalContext().getFlash().setKeepMessages(true);
			FacesMessage message = new FacesMessage("Zalogowano!");
			//FacesContext.getCurrentInstance().addMessage(null, message);
			suggestions = new ArrayList<Book>();
			for (Object o: SuggestionLogic.getInstance().getSuggestions(loggedUser).getResultList()){
				suggestions.add((Book) o);
			}
			

		} else {
			logged = false;
			FacesMessage message = new FacesMessage("Logowanie nie powiodło się!");
			FacesContext.getCurrentInstance().addMessage(null, message);
		}

		result = logicRespond.getDescription();

		return "MyPage.xhtml?faces-redirect=true";

	}

	public String logOut() {
		loggedUser = null;
		logged = false;
		loanList = null;
		clearAll();
		return "MyPage.xhtml?faces-redirect= true";
	}

	public void createLoanList(){
		ArrayList<Loan> list = new ArrayList<Loan>();
		loanList= new ArrayList<Item>();
		
		for (Object l : LoanLogic.getInstance().getUnfinishedUserLoans(loggedUser).getResultList()){
			list.add((Loan) l);			
		}
		for(Loan l : list){
			Integer itemId= l.getItemId();
			loanList.add((Item) RepoLogic.getInstance().getItem(itemId).getResultObject());
		}
	}

	public void createUserRepoList(){
		userRepoList= new ArrayList<Repo>();
		UserLogic logic= UserLogic.getInstance();
		List<Object> list= logic.getUserRepos(loggedUser).getResultList();
		for(Object o: list){
			userRepoList.add((Repo)o);
		}
	}
	
	public void createAvailableRepos(){
		availableRepos= new ArrayList<Repo>();
		RepoLogic logic= RepoLogic.getInstance();
		List<Object> list= logic.getAvailableRepos(loggedUser).getResultList();
		for(Object o: list){
			availableRepos.add((Repo)o);
		}
	}
	
	public void createMyBooks(){
		myBooks= new ArrayList<Item>();
		UserLogic logic= UserLogic.getInstance();
		
		
		for(Item i: loggedUser.getItems()){
			myBooks.add(i);
			
		
		}
	}
	
	public void createBooks(){
		
	}
	
	private void clearAll() {
		this.password = "";
		this.login = "";
		this.result = "";
	}

}
